<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/admin/home">首页</el-breadcrumb-item>
      <el-breadcrumb-item>博客管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="article-container">
      <el-row>
        <el-col :span="10">
          <!-- 按钮 -->
          <el-button type="primary" size="mini" icon="el-icon-circle-plus-outline" @click="addArcticle"> 添加博客 </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="delectBlogId"> 删除博客 </el-button>
        </el-col>
        <el-col :span="8" :offset="6">
          <el-input size="small" placeholder="请输入搜索关键字" prefix-icon="el-icon-search" v-model="searchMsg"></el-input>
          <el-button type="primary" size="mini" @click="searchBlog">搜索</el-button>
        </el-col>
      </el-row>
      <el-table border stripe :data="article" @selection-change="selectId">
        <el-table-column type="selection"></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="作者" prop="author"></el-table-column>
        <el-table-column label="标题" prop="tittle"></el-table-column>
        <el-table-column label="创作时间" prop="create_time"></el-table-column>
        <el-table-column label="查看次数" prop="views"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="editorBlog(scope.row)">
              <i class="el-icon-edit"></i>
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="deleteOneById(scope.row)">
              <i class="el-icon-delete"></i>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'MyArticle',

  data() {
    return {
      article: [],
      page: 1,
      size: 5,
      total: 0,
      selectBlogId: [],
      searchMsg: ''
    }
  },
  created() {
    this.getArticle()
  },
  mounted() {},

  methods: {
    async getArticle() {
      // const { result } = await this.$http.get(`/article?page=${this.page}&size=${this.size}`)
      const { result, page } = await this.$http.get('/article', { params: { page: this.page, size: this.size } })
      this.page = Number(page)
      this.article = result.data
      this.total = result.total
    },
    handleSizeChange(val) {
      this.size = val
      this.getArticle()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getArticle()
    },
    addArcticle() {
      this.$router.push('/article/add')
    },
    selectId(val) {
      this.selectBlogId = []
      val.forEach(item => {
        this.selectBlogId.push(item.id)
      })
    },
    async delectBlogId() {
      if (this.selectBlogId.length == 0) {
        this.$message({
          type: 'error',
          message: '请选择要删除的博客',
          duration: 1000
        })
        return
      }
      const data = await this.$http.delete('/article', { data: { selectBlogId: this.selectBlogId } })
      this.$message({
        type: 'success',
        message: data.message,
        duration: 1000
      })
      this.getArticle()
    },
    async deleteOneById(val) {
      const id = val.id
      const data = await this.$http.delete(`/article/${id}`)
      this.$message({
        type: 'success',
        message: data.message,
        duration: 1000
      })
      this.getArticle()
    },
    editorBlog(val) {
      const id = val.id
      this.$router.push({ name: 'editor', params: { id: id } })
    },
    async searchBlog() {
      const { result } = await this.$http.get('/article/search/blog', { params: { page: this.page, size: this.size, searchMsg: this.searchMsg } })
      if(result.data.length==0){
        this.$message({
          type: 'error',
          message: '没有查询到相关博客',
          duration: 1000
        })
        this.searchMsg = ''
        return
      }
      this.article = result.data
      this.searchMsg = ''
    }
  }
}
</script>

<style scoped>
.article-container {
  margin-top: 20px;
  min-height: 600px;
}
.el-input {
  width: 70%;
  margin-right: 10px;
  margin-bottom: 10px;
}
</style>
